<?php

namespace app\site\controller\site;

use app\admin\service\SetService;
use app\common\exception\MiException;
use app\site\controller\AController;
use app\site\service\site\ArticleCateService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Request;
use think\response\Json;

class ArticleCateController extends AController
{


    /**
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function index(): Json
    {
        $ser = ArticleCateService::instance();
        $list = $this->reqSer->setService($ser)->setSelect()->getList();
        $tree = $ser->childrenTree($list);
        return jsonSuccess([
            'items' => $tree,
        ]);
    }

    /**
     * @return Json|void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws MiException
     * @throws ModelNotFoundException
     */
    public function edit(){
        $ser = ArticleCateService::instance();
        if (\request()->isPost()){
            return $this->reqSer->setService($ser)->edit()->getResponse();
        }else{
            return $this->reqSer->setService($ser)->setFind()->getResponse();
        }
    }

    /**
     * @throws MiException
     */
    public function add(){
        if (\request()->isPost()){
            return  $this->reqSer->setService(ArticleCateService::instance())->add()->getResponse();
        }else{
            return jsonSuccess(REQUEST_ERROR);
        }
    }

    /**
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws MiException
     */
    public function del(){
        return $this->reqSer->setService(ArticleCateService::instance())->delete()->getResponse();
    }

}
